import { Controller, Get, Post, Body, Req, Query, HttpException, HttpStatus } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';

import { PointService } from './point.service';
import { CreatePointDto } from './dto/point.dto';
import { AdminPointPageReqDto } from './dto/admin-point.dto';

@ApiTags('小程序/积分')
@Controller('app/point')
export class PointController {
  constructor(private readonly pointService: PointService) {}

  /**
   * 添加积分
   */
  @Post()
  async addPoint(@Req() req: Request, @Body() data: CreatePointDto) {
    if (!req.user?.userId) {
      throw new HttpException('用户未登录', HttpStatus.UNAUTHORIZED);
    }
    await this.pointService.createAndAddUserPoint(req.user?.userId, data);
  }

  /**
   * 积分列表
   */
  @Get()
  pointList(@Query() query: AdminPointPageReqDto) {
    return this.pointService.findPage(query);
  }
}
